package com.cryptape.cita_wallet.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alm.token.R;
import com.cryptape.cita_wallet.language.LanguageType;
import com.cryptape.cita_wallet.language.MultiLanguageUtil;
import com.cryptape.cita_wallet.view.TitleBar;

import java.util.ArrayList;
import java.util.List;

;

/**
 * Created by dengming on 13/1/20.
 */

public class SwitchLanguageActivity extends NBaseActivity implements View.OnClickListener {

    private RecyclerView languageListRecycler;
    private List<String> languageArray = new ArrayList<>();
    private TitleBar title;


    @Override
    protected int getContentLayout() {
        return R.layout.activity_switch_language;
    }

    @Override
    protected void initView() {
        languageListRecycler = findViewById(R.id.rv_language_list);
        title = findViewById(R.id.title);
    }

    //    LanguageKeeper mLanguageKeeper;
//    private String mLanguageType;
    private String mDefaultType;
    private int mLanguageType;

    @Override
    protected void initData() {
//        mLanguageKeeper = new LanguageKeeper(this);
//        mLanguageType = LanguageKeeper.getInstance().getLanguage(this);
        mLanguageType = MultiLanguageUtil.getInstance().getLanguageType();

        languageArray.add("English");
        languageArray.add("简体中文");
        languageArray.add("日本語");
        languageArray.add("한국어");

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        languageListRecycler.setLayoutManager(linearLayoutManager);

        Adapter adapter = new Adapter();
        languageListRecycler.setAdapter(adapter);
        languageListRecycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        title.setOnLeftClickListener(() -> finish());

    }

    @Override
    protected void initAction() {

    }

    @Override
    public void onClick(View v) {

    }

    class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

        @NonNull
        @Override
        public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_switch_language, parent, false);
            return new Adapter.ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
            holder.language.setText(languageArray.get(position));
            if (mLanguageType== LanguageType.LANGUAGE_EN) {
                if (position == 0) {
                    holder.cb_select_language.setVisibility(View.VISIBLE);
                }
//                else{
//                    holder.cb_select_language.setVisibility(View.GONE);
//                }
            } else if (mLanguageType== LanguageType.LANGUAGE_CHINESE_SIMPLIFIED) {
                if (position == 1) {
                    holder.cb_select_language.setVisibility(View.VISIBLE);
                }
//                else{
//                    holder.cb_select_language.setVisibility(View.GONE);
//                }
            } else if (mLanguageType== LanguageType.LANGUAGE_JAPAN) {
                if (position == 2) {
                    holder.cb_select_language.setVisibility(View.VISIBLE);
                }
//                else{
//                    holder.cb_select_language.setVisibility(View.GONE);
//                }
            } else if (mLanguageType== LanguageType.LANGUAGE_KO) {
                if (position == 3) {
                    holder.cb_select_language.setVisibility(View.VISIBLE);
                }
//                else{
//                    holder.cb_select_language.setVisibility(View.GONE);
//                }
            }
//            int languageType = MultiLanguageUtil.getInstance().getLanguageType() ;
//            if()
            holder.root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if (position == 0) {
//                        mLanguageType = LanguageKeeper.LANGUAGE_EN;
//                    } else if (position == 1) {
//                        mLanguageType = LanguageKeeper.LANGUAGE_CN;
//                    } else if (position == 2) {
//                        mLanguageType = LanguageKeeper.LANGUAGE_JA;
//                    } else if (position == 3) {
//                        mLanguageType = LanguageKeeper.LANGUAGE_KO;
//                    }
//                    LanguageKeeper.getInstance().setLoaclLanguage(SwitchLanguageActivity.this, mLanguageType);
//                    finish();
                    int selectedLanguage = LanguageType.LANGUAGE_EN;
                    if(position == 0) {
                        selectedLanguage = LanguageType.LANGUAGE_EN;
                    }else if(position == 1){
                        selectedLanguage = LanguageType.LANGUAGE_CHINESE_SIMPLIFIED;
                    }else if(position == 2){
                        selectedLanguage = LanguageType.LANGUAGE_JAPAN;
                    }else if(position == 3){
                        selectedLanguage = LanguageType.LANGUAGE_KO;
                    }

                    MultiLanguageUtil.getInstance().updateLanguage(selectedLanguage);
                    Intent intent = new Intent(SwitchLanguageActivity.this, SplashActivity.class);
//                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                }
            });
            //            Currency currency = currencyArray.get(position);
//            if (SharePrefUtil.getString(ConstantUtil.CURRENCY, ConstantUtil.DEFAULT_CURRENCY).equals(currency.getName())) {
//                holder.currency.setRightImageShow(true);
//            } else {
//                holder.currency.setRightImageShow(false);
//            }
//            holder.currency.setNameText(currency.getName());
//            holder.currency.setOnClickListener(() -> {
//                SharePrefUtil.putString(ConstantUtil.CURRENCY, currency.getName());
//                notifyDataSetChanged();
//                finish();
//            });
        }

        @Override
        public int getItemCount() {
            return languageArray.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            TextView language;
            View root;
            ImageView cb_select_language;

            public ViewHolder(View view) {
                super(view);
                root = view.findViewById(R.id.root);
                language = view.findViewById(R.id.language);
                cb_select_language = view.findViewById(R.id.cb_select_language);
            }
        }
    }
}
